/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.thrift.ui.builder;

import static org.eclipse.core.resources.IMarker.LINE_NUMBER;
import static org.eclipse.core.resources.IMarker.MESSAGE;
import static org.eclipse.core.resources.IMarker.SEVERITY;
import static org.eclipse.core.resources.IMarker.SEVERITY_ERROR;
import static org.eclipse.core.resources.IResource.DEPTH_INFINITE;
import static org.eclipse.xtext.ui.MarkerTypes.FAST_VALIDATION;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;

/**
 * Factory for creating Error-Markers in an IFile.
 * 
 * @author Thomas Kistel
 * @version $Revision: 756 $
 */
class ThriftMarkerFactory {

    private static final String THRIFT_MARKER_ID = "de.thwildau.tm.moses.thrift.ui.thriftMarker";

    private final IFile file;
    private final IMarker[] markers;


    ThriftMarkerFactory(IFile file) throws CoreException {
        this.file = file;
        file.deleteMarkers(THRIFT_MARKER_ID, true, DEPTH_INFINITE);
        markers = file.findMarkers(FAST_VALIDATION, true, DEPTH_INFINITE);
    }

    void createMarkerIfNecessary(String fileName, String message, int lineNumber) throws CoreException {
        String location = file.getLocation().toOSString();
        if (!location.endsWith(fileName) || containsMarker(message, lineNumber))
            return;
        IMarker marker = file.createMarker(THRIFT_MARKER_ID);
        marker.setAttribute(SEVERITY, SEVERITY_ERROR);
        marker.setAttribute(MESSAGE, message);
        marker.setAttribute(LINE_NUMBER, lineNumber);
    }

    private boolean containsMarker(String description, int lineNumber) throws CoreException {
        for (IMarker marker : markers) {
            String markerMessage = (String)marker.getAttribute(MESSAGE);
            if (markerMessage.equalsIgnoreCase(description) && marker.getAttribute(LINE_NUMBER).equals(lineNumber))
                return true;
        }
        return false;
    }
}
